package com.ygl.lucene.service;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author yanggld
 * @Date 2019/5/16-10:01
 * @Description
 */
public class LuceneService {

    /**
     * 索引位置
     */
    private String indexPath = "D:\\Lucene_index";
    /**
     * 原始文件位置
     */
    private String filePath = "D:\\Lucene_Document";

    private List<String> getIndexContent(){
        List<String> contentList = new ArrayList<>();
        contentList.add("企业的文化是什么呢？");
        contentList.add("企业的员工怎么样呢？");
        return contentList;
    }

    /**
     * 创建索引
     * @throws Exception
     */
    public void createIndex() throws Exception{
        //指定索引存放的位置
        //E:\Lucene_index
        Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
        System.out.println("pathname："+Paths.get(new File(indexPath).getPath()));
        //创建一个分词器
        //        StandardAnalyzer analyzer = new StandardAnalyzer();
        //        CJKAnalyzer cjkAnalyzer = new CJKAnalyzer();
        SmartChineseAnalyzer smartChineseAnalyzer = new SmartChineseAnalyzer();
        //创建indexwriterConfig(参数分词器)
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(smartChineseAnalyzer);
        //创建indexwrite 对象(文件对象，索引配置对象)
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        //原始文件
        List<String> indexContent = getIndexContent();
        for (String content : indexContent) {
            System.out.println("内容："+content);
            Field contentField = new TextField("content", content, Field.Store.YES);
            //创建Document 对象
            Document indexableFields = new Document();
            indexableFields.add(contentField);
            //创建索引，并写入索引库
            indexWriter.addDocument(indexableFields);
        }
        //关闭indexWriter
        indexWriter.close();
    }

    /**
     * 关键词查询索引
     * @param text 关键词
     */
    public void searchIndex(String text) throws Exception {
        //指定索引库存放路径
        Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
        //创建indexReader对象
        IndexReader indexReader = DirectoryReader.open(directory);
        //创建indexSearcher对象
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        //lucence查询解析器，用于指定查询的属性名和分词器
        //中文分词
        Analyzer analyzer = new IKAnalyzer();
        QueryParser parser = new QueryParser("content", analyzer);
        //用户输入内容
        Query query = parser.parse(text);
        System.out.println(query.toString());
//        //创建查询
//        Query query = new TermQuery(new Term("content", text));
        //执行查询
        //参数一  查询对象    参数二  查询结果返回的最大值
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("查询结果的总数"+topDocs.totalHits);
        //遍历查询结果
        for (ScoreDoc scoreDoc: topDocs.scoreDocs){
            //scoreDoc.doc 属性就是doucumnet对象的id
            Document doc = indexSearcher.doc(scoreDoc.doc);
            System.out.println(doc.getField("content"));
        }
        indexReader.close();
    }


    public void deleteIndex(){

    }

}
